package serializer

import (
	"QImages/database"
	"QImages/entity"
	"QImages/utils"
)

// Comment 前端显示的评论信息
type Comment struct {
	ID        uint   `json:"id"`
	CreatedAt string `json:"created_at"`
	ComName   string `json:"com_name"`
	ComId     uint   `json:"com_id"`
	ParentID  uint   `json:"parent_id"`
	Content   string `json:"content"`
}

// BuildComment 转化为前端显示的评论信息
func BuildComment(dbComment *entity.Comment) Comment {
	var res Comment
	res.ID = dbComment.ID
	res.CreatedAt = utils.FormatDatetime(dbComment.CreatedAt)
	res.Content = dbComment.Content
	res.ComId = dbComment.UserID

	var dbUser entity.User
	if err := database.DB.Where("id = ?", dbComment.UserID).Find(&dbUser).Error; err != nil {
		panic("评论用户 id 异常")
	}
	res.ComName = dbUser.Name
	return res
}
